<template>
  <el-drawer class="form-drawer" @closed="closed" v-bind="$attrs" v-on="$listeners">
    <el-form ref="drawerForm" :model="formData" :rules="rules">
      <div class="form-content">
        <el-form-item prop="date" label="日期：" :label-width="labelWidth">
          <el-date-picker
            v-model="formData.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="name" label="姓名：" :label-width="labelWidth">
          <LimitInput v-model="formData.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item prop="sex" label="性别：" :label-width="labelWidth">
          <el-select v-model="formData.sex" placeholder="请选择性别">
            <el-option label="男" value="male"></el-option>
            <el-option label="女" value="female"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="desc" label="简介：" :label-width="labelWidth">
          <LimitInput v-model="formData.desc" placeholder="请输入简介" :rows="3" />
        </el-form-item>
        <el-form-item prop="pic" label="照片：" :label-width="labelWidth">
          <UploadImg v-model="formData.pic" />
        </el-form-item>
      </div>
      <div class="form-handle">
        <el-button type="default" size="small" @click="handeClick('cancel')">取消</el-button>
        <el-button type="primary" size="small" @click="handeClick('submit')">确定</el-button>
      </div>
    </el-form>
  </el-drawer>
</template>
<script>
import { pickerOptionsShortcuts } from '@/constants';
import { valid } from 'mockjs';
export default {
  inheritAttrs: false,
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      labelWidth: '100px',
      formData: {
        date: '',
        name: '',
        age: '',
        desc: '',
        sex: '',
      },
      attrs: undefined,
      listeners: undefined,
      pickerOptions: {
        shortcuts: pickerOptionsShortcuts.slice(2),
      },
      rules: {
        date: [{ required: true, message: '请选择日期', trigger: ['blur', 'change'] }],
        name: [{ required: true, message: '请输入姓名', trigger: ['blur', 'change'] }],
        sex: [{ required: true, message: '请选择性别', trigger: ['blur', 'change'] }],
        desc: [{ required: false, message: '请输入描述', trigger: ['blur', 'change'] }],
        pic: [{ required: true, message: '请上传照片', trigger: ['blur', 'change'] }],
      },
    };
  },
  watch: {
    visible(newVal) {},
  },
  created() {},
  methods: {
    closed() {
      this.$refs.drawerForm && this.$refs.drawerForm.clearValidate();
      this.clearFormData();
    },
    handeClick(type) {
      switch (type) {
        case 'cancel':
          this.$emit('update:visible', false);
          break;
        case 'submit':
          this.submit();
          break;
      }
    },
    submit() {
      this.$refs.drawerForm &&
        this.$refs.drawerForm.validate((valid, err) => {
          if (valid) {
            this.$emit('submit', this.formData);
          }
        });
    },
    clearFormData() {
      Object.keys(this.formData).forEach(k => {
        this.formData[k] = undefined;
      });
    },
  },
};
</script>
<style lang="less" scoped>
.form-drawer {
  /deep/ .el-drawer__header {
    padding: 10px 15px;
    margin-bottom: 0;
    border-bottom: 1px solid #f2f6fc;
  }
  /deep/ .el-drawer__close-btn {
    outline: none;
  }
  .el-form {
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: space-between;
  }
  .form-content {
    padding: 25px 15px;
    overflow: auto;
    .el-range-editor {
      width: 100%;
    }
  }
  .form-handle {
    padding: 15px;
    text-align: right;
    box-shadow: rgba(0, 0, 0, 0.05) 0 -2px 5px 0;
  }
}
</style>
